package com.novel.authorization.staff.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.novel.authorization.staff.entity.NfGroup;
import com.novel.authorization.staff.service.NfGroupService;
import com.novel.base.entity.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.validation.Valid;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色表
 *
 * @author 樊温军
 * @date Wed May 07 11:00:53 CST 2025
 */
@Slf4j
@Api(tags = "角色表", value = "角色表")
@RestController
@AllArgsConstructor
@RequestMapping("/nfGroup")
public class NfGroupController {

    private final NfGroupService nfGroupService;

    /**
     * 角色表—列表
     *
     * @param nfGroup 角色表
     * @return R<List < NfGroup>>
     */
    @SaCheckPermission("nfGroup:list")
    @ApiOperation(value = "角色表—列表", notes = "角色表—列表")
    @GetMapping("/list")
    public R<List<NfGroup>> getNfGroupList(NfGroup nfGroup) {
        log.info("开始查询角色表—列表，参数：{}", nfGroup);
        try {
            return R.ok(nfGroupService.getNfGroupList(nfGroup), "查询成功");
        } catch (Exception e) {
            log.error("查询角色表—列表时发生错误：{}", e.getMessage(), e);
            return R.failed("查询角色表—列表时发生错误，请稍后重试！");
        }
    }

    /**
     * 角色表—列表—分页
     *
     * @param page   分页对象
     * @param nfGroup 角色表
     * @return R<IPage < NfGroup>>
     */
    @SaCheckPermission("nfGroup:page")
    @ApiOperation(value = "角色表—列表—分页", notes = "角色表—列表—分页")
    @GetMapping(value = "/page")
    public R<IPage<NfGroup>> getNfGroupPage(Page<NfGroup> page, NfGroup nfGroup) {
        log.info("开始查询角色表—列表—分页，第 {} 页，每页 {} 条记录，其他参数：{}", page.getCurrent(), page.getSize(), nfGroup);
        try {
            IPage<NfGroup> iPage = nfGroupService.getPage(page, nfGroup);
            return R.ok(iPage, "查询成功");
        } catch (Exception e) {
            log.error("查询角色表—列表—分页时发生错误：{}", e.getMessage(), e);
            return R.failed("查询角色表—列表—分页时发生错误，请稍后重试！");
        }
    }

    /**
     * 角色表—保存
     *
     * @param nfGroup 角色表
     * @return R<Long>
     */
    @SaCheckPermission("nfGroup:saveOne")
    @ApiOperation(value = "角色表—保存", notes = "角色表—保存")
    @PostMapping(value = "/saveOne")
    public R<Long> saveOneNfGroup(@Valid @RequestBody NfGroup nfGroup) {
        log.info("开始保存角色表：{}", nfGroup);
        try {
            Long id = nfGroupService.saveOneNfGroup(nfGroup);
            log.info("角色表保存成功，ID: {}", id);
            return R.ok(id, "保存成功");
        } catch (Exception e) {
            log.error("保存角色表时发生错误：{}", e.getMessage(), e);
            return R.failed("保存角色表时发生错误，请稍后重试！");
        }
    }

    /**
     * 角色表—删除
     *
     * @param id 角色表主键
     * @return R<Boolean>
     */
    @SaCheckPermission("nfGroup:delById")
    @ApiOperation(value = "角色表—删除", notes = "角色表—删除")
    @PostMapping(value = "/delById")
    public R<Boolean> delByIdNfGroup(Long id) {
        log.info("开始删除角色表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            nfGroupService.delById(id);
            return R.ok(Boolean.TRUE, "删除成功");
        } catch (Exception e) {
            log.error("删除角色表失败: ID={}, Error={}", id, e.getMessage(), e);
            return R.failed("删除失败，请稍后重试！");
        }
    }

    /**
     * 角色表—详情
     *
     * @param id 角色表主键
     * @return R<NfGroup>
     */
    @SaCheckPermission("nfGroup:details")
    @ApiOperation(value = "角色表—详情", notes = "角色表—详情")
    @GetMapping(value = "/details")
    public R<NfGroup> getNfGroup(Long id) {
        log.info("开始查询角色表：{}", id);
        if (id == null || id <= 0) {
            log.warn("ID为空或无效: {}", id);
            return R.failed("请传入有效的ID！");
        }
        try {
            NfGroup nfGroup = nfGroupService.queryById(id);
            if (nfGroup != null) {
                return R.ok(nfGroup, "查询成功");
            } else {
                return R.failed("未找到角色表");
            }
        } catch (Exception e) {
            log.error("查询角色表详情时发生错误：{}", e.getMessage(), e);
            return R.failed("查询角色表详情时发生错误，请稍后重试！");
        }
    }
}
